package com.agnes.phone.listener;

import android.content.Context;
import android.support.v7.app.AlertDialog;
import android.telephony.PhoneStateListener;
import android.telephony.ServiceState;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.Log;

public class MyPhoneStateListener extends PhoneStateListener {
    private String TAG = "MyPhoneStateListener";

    private Context mContext;

    public MyPhoneStateListener(Context context) {
        mContext = context;
    }

    @Override
    public void onServiceStateChanged(ServiceState serviceState) {
        super.onServiceStateChanged(serviceState);
        Log.e(TAG, "MyPhoneStateListener onServiceStateChanged: " +
                serviceState.toString());
    }

    @Override
    public void onCallStateChanged(int state, String phoneNumber) {
        Log.e(TAG, "MyPhoneStateListener state: "
                + state + " phoneNumber: " + phoneNumber);
        switch (state) {
            case TelephonyManager.CALL_STATE_IDLE:      // 电话挂断or空闲
                Log.e(TAG,"MyPhoneStateListener call idle");
                break;
            case TelephonyManager.CALL_STATE_RINGING:   // 电话响铃
                Log.e(TAG, "MyPhoneStateListener onCallStateChanged endCall");
               /* HangUpTelephonyUtil.endCall(mContext);*/
                break;
            case TelephonyManager.CALL_STATE_OFFHOOK:   // 来电接通 或者 去电，去电接通  但是没法区分
                Log.e(TAG,"MyPhoneStateListener call offhook");
                break;
        }


    }


     /* if (!TextUtils.isEmpty(phoneNumber)) {
        new AlertDialog.Builder(mContext)
                .setTitle("来电拦截信息")
                .setMessage("来电号码：" + phoneNumber)
                .setPositiveButton("确定", (dialog, which) -> dialog.dismiss())
                .setCancelable(false)
                .create()
                .show();
    }*/


   /* @SuppressLint("CheckResult")
    private String getContactNameFromPhoneBook(Context context, String incomingNumber) {
        RxPermissions rxPermissions = new RxPermissions((MainActivity) mContext);
        final String[] contactName = {""};
        rxPermissions
                .request(Manifest.permission.CAMERA)
                .subscribe(granted -> {
                    if (granted) { // Always true pre-M
                        String[] projection = {ContactsContract.PhoneLookup.DISPLAY_NAME,
                                ContactsContract.PhoneLookup.NUMBER};
                        Uri uri = Uri.withAppendedPath(
                                ContactsContract.PhoneLookup.CONTENT_FILTER_URI,
                                Uri.encode(incomingNumber));
                        Cursor cursor = context.getContentResolver().query(uri, projection, null, null, null);
                        if (cursor.moveToFirst()) {
                            contactName[0] = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                            cursor.close();
                        }

                    }
                });
        return contactName[0];
    }*/
}


